home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 2 / ETO Development Tools 2.iso / Tools - Objects / ResEdit / ResEdit 2.0b2 / Examples / CExamples / Source / XXXX.Edit.c < prev   
Encoding:
C/C++ Source or Header  |  1990-04-02  |  9.0 KB  |  301 lines  |  [TEXT/MPS ]

  1. /*
  2. File ResXXXXEd.c
  3.  
  4. Copyright Apple Computer, Inc. 1985-1990
  5. All rights reserved.
  6. */
  7.  
  8. #include    <types.h>
  9. #include    <memory.h>
  10. #include    <menus.h>
  11. #include    <resources.h>
  12.  
  13. #include    "ResEd.h"
  14.  
  15. #define        windowWidth            300
  16. #define        windowHeight        100
  17. #define        deleteChr            8
  18. #define        sizeOfMyResource    10
  19.  
  20. typedef struct rXXXXRec
  21.     {
  22.     ParentHandle    father;        /* Back ptr to dad */
  23.     Str255            name;        /* The name of this editor */
  24.     WindowPtr        windPtr;    /* This view's window */
  25.     Boolean            rebuild;    /* Set true if things have changed */
  26.     Handle            hXXXX;        /* The resource we are working on */
  27.     Boolean            resWasntLoaded; /* True if the resource wasn't loaded before we started. */
  28.     } rXXXXRec;
  29.  
  30. typedef rXXXXRec    *rXXXXPtr;
  31. typedef rXXXXPtr    *rXXXXHandle;
  32.  
  33. /* Function prototypes. */
  34. pascal void DoMenu(short menu, short item, rXXXXHandle myXXXX);
  35.  
  36. /*- -    -  -  - -  -  -  - -  -  -    - -  -    -  - -    -  -  - -  -  -*/
  37.  
  38. /* Fix up the window name and title for our window. */
  39. void GetNameAndTitle(StringPtr windowTitle,StringPtr windowName,Handle thing)
  40.     {
  41.     strcpy(windowTitle,"\pXXXX");
  42.     SetETitle(thing,windowTitle);
  43.     strncpy(windowName,windowTitle,*windowTitle + 1);    /* Add 1 for the length byte */
  44.     }
  45.  
  46. /*- -  -  -    - -  -    -  - -    -  -  - -  -  -  - -  -  -    - -  -    -*/
  47.  
  48. pascal void EditBirth(Handle thing,ParentHandle dad)
  49.     {
  50.     rXXXXHandle        myXXXX;
  51.     WindowPtr        myWindow;
  52.     Str255            windowTitle,newName;
  53.  
  54.     /* Prepare window title and request creation of a new window */
  55.     GetNameAndTitle(windowTitle, newName, thing);
  56.     myWindow = EditorWindSetup(noDialog, false, windowWidth, windowHeight, windowTitle, newName, true, ResEdID(), dad);
  57.  
  58.     /* If we got a new window, then start up the editor */
  59.     if (myWindow != NULL)
  60.         {
  61.         /* This was called via a NEW, so make a new resource. */
  62.         if ((GetHandleSize(thing)) == 0L)
  63.             FixHand(sizeOfMyResource,thing);
  64.  
  65.         /* Get memory for and handle to our instance record */
  66.         myXXXX = (rXXXXHandle)NewHandle(sizeof(rXXXXRec));
  67.         BubbleUp((Handle)myXXXX);
  68.         HLock((Handle)myXXXX);
  69.  
  70.         /* Put information about this incarnation of the editor and the window it is */
  71.         /* serving into our record.(always passed around in the handle myXXXX).     */
  72.  
  73.         (*myXXXX)->windPtr = myWindow;
  74.         (*myXXXX)->father = dad;
  75.  
  76.         /* Use memcpy here since the editorNameChr is a null and would stop strncpy. */
  77.         memcpy((*myXXXX)->name, newName, newName[0] + 1);    /* Add 1 for the length byte */
  78.         (*myXXXX)->hXXXX = thing;
  79.  
  80.         (*myXXXX)->resWasntLoaded = !WasItLoaded;
  81.                 
  82.         /* Let the main program know who is to manage this window by giving it both    */
  83.         /* our resource ID number and our instance record handle.        */
  84.         ((WindowPeek)myWindow)->refCon = (long)myXXXX;
  85.  
  86.         /* Set up any menus,views, etc. for this window here. */
  87.  
  88.         HUnlock((Handle)myXXXX);
  89.         }
  90.     }
  91.  
  92. /*- -  -  -    - -  -    -  - -    -  -  - -  -  -  - -  -  -    - -  -    -*/
  93.  
  94. /* Not used for editors. */
  95. pascal void PickBirth(ResType t, ParentHandle dad)
  96.     {
  97. #pragma    unused (t, dad)
  98.     }
  99.  
  100. /*- -  -  -    - -  -    -  - -    -  -  - -  -  -  - -  -  -    - -  -    -*/
  101.  
  102. pascal void DoEvent(EventRecord *evt, rXXXXHandle myXXXX)
  103.     {
  104.     Point        mousePoint;
  105.  
  106.     BubbleUp((Handle)myXXXX);        /* Move our item up in memory */
  107.     HLock((Handle)myXXXX);            /* Lock it down */
  108.  
  109.     /* Handle event passed to us by main program.  Just like a 'real' event loop, except…    */
  110.     /* there is no loop and we don't have to handle as much because the main program    */
  111.     /* will do all the stuff that doesn't apply to us.                    */
  112.  
  113.     mousePoint = evt->where;        /* Point at which the event occured */
  114.     SetPort((*myXXXX)->windPtr);    /* Set the port to our window */
  115.     GlobalToLocal(&mousePoint);        /* Convert event location to local coords */
  116.  
  117.     switch (evt->what)
  118.         {
  119.     case mouseDown:
  120.         break;                        /* Do any special mouse down processing here. */
  121.  
  122.     case activateEvt:
  123.         AbleMenu(fileMenu, fileAll);
  124.         AbleMenu(rsrcMenu, rsrcEditor);
  125.         if (evt->modifiers & activeFlag)
  126.             {
  127.             /* Do any activate processing here (such as inserting a menu) */
  128.             }
  129.         else
  130.             {
  131.             /* Do any deactivate processing here (such as deleting a menu). */
  132.             }
  133.         break;
  134.  
  135.     case updateEvt:
  136.         /* Do the appropriate update processing here.  Remember that BeginUpdate has already been called. */
  137.         PaintRect(&(*myXXXX)->windPtr->portRect);
  138.         break;
  139.  
  140.     case keyDown:
  141.         /* Do any key processing here. */
  142.         if ((evt->message & charCodeMask) == deleteChr)    /* Convert the delete character into a clear command. */
  143.             {
  144.             DoMenu(editMenu, clearItem, myXXXX);
  145.             }
  146.         break;
  147.  
  148.     case nullEvent:
  149.         /* Do any null event processing here (such as blinking a cursor).    */
  150.         break;
  151.         }
  152.     HUnlock((Handle)myXXXX);
  153.     }
  154. /*- -  -  -    - -  -    -  - -    -  -  - -  -  -  - -  -  -    - -  -    -*/
  155.  
  156. pascal void DoInfoUpdate(short oldID, short newID, rXXXXHandle myXXXX)
  157.     {
  158.     Str255        windowTitle,windowName;
  159.  
  160.     /* Since our ID has changed, we need to change our window title */
  161.     GetNameAndTitle(windowTitle, windowName, (Handle)((*myXXXX)->hXXXX));
  162.     GetWindowTitle(windowTitle, windowName,true, (*myXXXX)->father);
  163.  
  164.     /* Save the new name in my data structure. */
  165.     /* Use memcpy here since the editorNameChr is a null and would stop strncpy. */
  166.     memcpy((*myXXXX)->name, windowName, windowName[0] + 1);    /* Add 1 for the length byte. */
  167.  
  168.     SetWTitle((*myXXXX)->windPtr, windowTitle);    /* Set the new window title. */
  169.  
  170.     /* Now, let our father object know that our ID has been changed */
  171.     (*((*myXXXX)->father))->rebuild = true;            /* Rebuild the picker list. */
  172.     CallInfoUpdate(oldID, newID, (*((*myXXXX)->father))->wind->refCon, (*((*myXXXX)->father))->wind->windowKind);
  173.     }
  174.  
  175. /*- -  -  -    - -  -    -  - -    -  -  - -  -  -  - -  -  -    - -  -    -*/
  176.  
  177. pascal Boolean IsThisYours(Handle thing, rXXXXHandle myXXXX)
  178.     {
  179.     return (thing == (Handle)((*myXXXX)->hXXXX));
  180.     }
  181.  
  182. /*- -  -  -    - -  -    -  - -    -  -  - -  -  -  - -  -  -    - -  -    -*/
  183.  
  184. /* Close down the window and get rid of any memory that has been allocated. */
  185. static Boolean DoClose(rXXXXHandle myXXXX)
  186.     {
  187.     PassMenu (fileMenu, closeItem, (ParentHandle)myXXXX);
  188.     if (WasAborted())
  189.         {
  190.         return (false);
  191.         }
  192.         else
  193.         {
  194.         CloseWindow((*myXXXX)->windPtr);
  195.         WindReturn((*myXXXX)->windPtr);    /* Mark the window record as being available */
  196.         SetTheCursor(arrowCursor);    /* Make sure the cursor is the arrow cursor */
  197.     
  198.         /* Delete any menus that we added and redraw the menu bar.    */
  199.         /* Be sure to dispose of any handles you are done with.        */
  200.         /* Release the resource if we were launched from a picker. */
  201.         if (((*myXXXX)->resWasntLoaded) && ((*((*myXXXX)->father))->name[1] != editorNameChr))
  202.             ReleaseResource((Handle)(*myXXXX)->hXXXX);    /* Let it be free (if it is not changed)! */
  203.     
  204.         DisposHandle((Handle)myXXXX);
  205.         return (true);
  206.         }
  207.     }
  208.  
  209. /*- -  -  -    - -  -    -  - -    -  -  - -  -  -  - -  -  -    - -  -    -*/
  210.  
  211. pascal void DoMenu(short menu, short item, rXXXXHandle myXXXX)
  212.     {
  213.     short        saveRefNum;
  214.     Handle        hTemp;
  215.  
  216.     BubbleUp((Handle)myXXXX);
  217.     HLock((Handle)myXXXX);
  218.     SetPort((*myXXXX)->windPtr);            /* Set the port to our window */
  219.  
  220.     /* Again, we handle the menu stuff just as we would in a 'real' application    */
  221.     /* except that we only have to handle those items that apply to our editor.    */
  222.         
  223.     switch (menu)
  224.         {
  225.         case fileMenu:
  226.             switch (item)
  227.                 {
  228.                 case closeItem:
  229.                     if (DoClose(myXXXX))    /* Close our window                        */
  230.                         {
  231.                         return;                /* Return immediately since our resource is gone! */
  232.                         }
  233.                     break;
  234.  
  235.                 case saveItem:                /* Pass the save on to other windows.    */
  236.                     PassMenu(fileMenu,saveItem, (ParentHandle)myXXXX);
  237.                     break;
  238.  
  239.                 case printItem:
  240.                     PrintWindow(NULL);
  241.                     break;
  242.                 }
  243.         case rsrcMenu:
  244.             switch(item)
  245.                 {
  246.                 case rsrcRevertItem:
  247.                     if (NeedToRevert((*myXXXX)->windPtr, (Handle)(*myXXXX)->hXXXX))
  248.                         {
  249.                         /* The area under the window will need to be updated     */
  250.                         InvalRect(&(*myXXXX)->windPtr->portRect);
  251.         
  252.                         /* We will need to restore the current resource file reference number when we are done here.                */
  253.                         saveRefNum = CurrentRes();
  254.         
  255.                         /* We are going to be using the resource file we came from.    */
  256.                         UseResFile(HomeResFile((Handle)(*myXXXX)->hXXXX));
  257.         
  258.                         /* Read in the old copy from disk (see documentation for revertResource)    */
  259.                         /* Clear it out unless this was a newly created resource    */
  260.                         if (!RevertThisResource((ParentHandle)myXXXX, (Handle)(*myXXXX)->hXXXX))
  261.                             {
  262.                             /* The resource was newly added so we need to remove it.*/
  263.                             /* Save the handle so that we can dispose it later. */
  264.                             hTemp = (Handle)(*myXXXX)->hXXXX;
  265.         
  266.                             /* Make sure that the picker list is rebuilt to remove this item.                                         */
  267.                             (*((*myXXXX)->father))->rebuild = true;
  268.         
  269.                             if (DoClose(myXXXX))        /* Close the window.            */
  270.                                 {
  271.                                 RemoveResource(hTemp);    /* Dispose the resource itself. */
  272.                                 return;                    /* Since the resource is gone.    */
  273.                                 }
  274.                             }
  275.                         UseResFile(saveRefNum);            /* Go back to using the old resource file */
  276.                         }
  277.                     break;
  278.  
  279.                 case rsrcGetInfoItem:        /* Show GetInfo window */
  280.                     ShowInfo((Handle)(*myXXXX)->hXXXX,(ParentHandle)myXXXX);
  281.                     break;
  282.                 }
  283.     
  284.         case editMenu:            /* Implement the edit menu here    */
  285.             {
  286.             switch (item)
  287.                 {
  288.                 case cutItem:
  289.                     break;
  290.                 case copyItem:
  291.                     break;
  292.                 case pasteItem:
  293.                     break;
  294.                 case clearItem:
  295.                     break;
  296.                 }
  297.             }
  298.         }
  299.     }
  300.  
  301.